<?php
/*
 * 
 * Ucenter 整合接口
 * 
 * $Id$
 */

define ( 'UC_CLIENT_VERSION', '1.6.0' );
define ( 'UC_CLIENT_RELEASE', '20110501' );

define ( 'API_DELETEUSER', 1 );
define ( 'API_RENAMEUSER', 1 );
define ( 'API_GETTAG', 1 );
define ( 'API_SYNLOGIN', 1 );
define ( 'API_SYNLOGOUT', 1 );
define ( 'API_UPDATEPW', 1 );
define ( 'API_UPDATEBADWORDS', 1 );
define ( 'API_UPDATEHOSTS', 1 );
define ( 'API_UPDATEAPPS', 1 );
define ( 'API_UPDATECLIENT', 1 );
define ( 'API_UPDATECREDIT', 1 );
define ( 'API_GETCREDIT', 1 );
define ( 'API_GETCREDITSETTINGS', 1 );
define ( 'API_UPDATECREDITSETTINGS', 1 );
define ( 'API_ADDFEED', 1 );
define ( 'API_RETURN_SUCCEED', '1' );
define ( 'API_RETURN_FAILED', '-1' );
define ( 'API_RETURN_FORBIDDEN', '1' );

define ( 'IN_API', true );

define ( 'CURSCRIPT', 'api' );

class Uclient extends Action {
	/* (non-PHPdoc)
	 * @see Action::execute()
	 */
	public function execute($params) {
		init_uclient_config ();
		
		$get = $post = array ();
		
		$code = @$_GET ['code'];
		
		parse_str ( ntzauthcode ( $code, 'DECODE', UC_KEY ), $get );
		
		if (time () - $get ['time'] > 3600) {
			exit ( 'Authracation has expiried' );
		}
		
		if (empty ( $get )) {
			exit ( 'Invalid Request' );
		}
		$post = uc_xml_unserialize ( file_get_contents ( 'php://input' ) );
		elog ( 'uc_client :' . $get ['action'] );
		if (in_array ( $get ['action'], array ('test', 'deleteuser', 'renameuser', 'gettag', 'synlogin', 'synlogout', 'updatepw', 'updatebadwords', 'updatehosts', 'updateapps', 'updateclient', 'updatecredit', 'getcredit', 'getcreditsettings', 'updatecreditsettings', 'addfeed' ) )) {
			$uc_note = new UCNote ();
			$action = $get ['action'];
			if (is_callable ( array ($uc_note, $action ) )) {
				echo call_user_func_array ( array ($uc_note, $action ), array ($get, $post ) );
				exit ();
			} else {
				exit ( API_RETURN_FORBIDDEN );
			}
		} else {
			exit ( API_RETURN_FAILED );
		}
	}
}
/**
 * 
 * ucenter 通知处理器
 * @author LeoNing
 *
 */
class UCNote {
	public function __construct() {
	}
	function _serialize($arr, $htmlon = 0) {
		if (! function_exists ( 'xml_serialize' )) {
			include_once $this->path . '/uc_client/lib/xml.class.php';
		}
		return xml_serialize ( $arr, $htmlon );
	}
	function test($get, $post) { //与ucenter的通信测试
		return API_RETURN_SUCCEED;
	}
	/**
	 * 
	 * 同步登录
	 * @param array $get
	 * @param array $post
	 */
	function synlogin($get, $post) {
		if (! API_SYNLOGIN) {
			return API_RETURN_FORBIDDEN;
		}
		@header ( 'P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"' );
		$uid = intval ( $get ['uid'] );
		$vipM = new VipUserModel ();
		$vip ['account'] = $get ['username'];
		if ($vipM->exist ( $vip )) { //相同用户名的人在本系统不存在
			if ($vipM->login ( $get ['username'], $get ['password'] )) {
				$cookietime = 31536000;
				scookie ( 'vip_auth', ntzauthcode ( "{$get['username']}\t{$get['password']}", 'ENCODE' ), $cookietime );
				scookie ( 'vip_authed', 0, $cookietime );
			}
		}
	}
	/**
	 * 
	 * 同步退出
	 * @param array $get
	 * @param array $post
	 */
	function synlogout($get, $post) {
		if (! API_SYNLOGOUT) {
			return API_RETURN_FORBIDDEN;
		}
		@header ( 'P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"' );
		scookie ( 'vip_auth', null, - 31536000 );
		scookie ( 'vip_authed', null, - 31536000 );
	}
	/**
	 * 
	 * 删除用户
	 * @param array $get
	 * @param array $post
	 */
	function deleteuser($get, $post) {
		global $__global;
		if (! API_DELETEUSER) {
			return API_RETURN_FORBIDDEN;
		}
		$uids = safe_ids ( $get ['ids'], ',', true );
		$userModel = new VipUserModel ();
		$userModel->begin ();
		$__global ['delete_user_from_ucapi'] = true;
		$rst = apply_filter ( 'on_delete_vip_user', true, $uids );
		if ($rst) {
			$where ['uid IN'] = $uids;
			$rst = $userModel->delete ( $where );
			if ($rst !== false) {
				$userModel->commit ();
			} else {
				$userModel->rollback ();
			}
		}
		return API_RETURN_SUCCEED;
	}
	//改名
	function renameuser($get, $post) {
		if (! API_RENAMEUSER) {
			return API_RETURN_FORBIDDEN;
		}
		$oname = $get ['oldusername'];
		$data ['account'] = $get ['newusername'];
		$userModel = new VipUserModel ();
		if (! $userModel->exist ( $data )) {
			$userModel->update ( $data, array ('account' => $oname ) );
		}
		return API_RETURN_SUCCEED;
	}
	//修改密码
	function updatepw($get, $post) {
		if (! API_UPDATEPW) {
			return API_RETURN_FORBIDDEN;
		}
		$account = $get ['username'];
		$password = md5 ( $get ['password'] );
		$userModel = new VipUserModel ();
		$userModel->update ( array ('passwd' => $password ), array ('account' => $account ) );
		return API_RETURN_SUCCEED;
	}	
}
